///*
// * @(#)HttpUtils.java $version 2012. 4. 27.
// *
// * Copyright 2007 NHN Corp. All rights Reserved. 
// * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
// */
//
//package com.board.utils;
//
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.io.UnsupportedEncodingException;
//import java.util.ArrayList;
//import java.util.Iterator;
//import java.util.List;
//import java.util.Map;
//
//import org.apache.commons.httpclient.HttpException;
//import org.apache.http.HttpResponse;
//import org.apache.http.NameValuePair;
//import org.apache.http.client.ClientProtocolException;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.entity.UrlEncodedFormEntity;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.client.methods.HttpRequestBase;
//import org.apache.http.impl.client.DefaultHttpClient;
//import org.apache.http.message.BasicNameValuePair;
//import org.apache.http.params.HttpConnectionParams;
//import org.apache.http.params.HttpParams;
//import org.apache.http.protocol.HTTP;
//import org.springframework.web.util.HtmlUtils;
//
///**
// * HTTP 관련 util을 제공한다.
// * 
// * @author hs
// */
//public class HttpConnectionUtils {
//	private int httpConnectionTimeout;
//	private int httpSocketTimeout;
//	private String urlCharset;
//	private String responseContentCharset;
//
//	public HttpConnectionUtils() {
//		this.httpConnectionTimeout = 3000;
//		this.httpSocketTimeout = 3000;
//		this.urlCharset = HTTP.UTF_8;
//		this.responseContentCharset = HTTP.UTF_8;
//	}
//
//	public HttpConnectionUtils(int connectionTimeout, int socketTimeout, String urlCharset, String responseContentCharset) {
//		this.httpConnectionTimeout = connectionTimeout;
//		this.httpSocketTimeout = socketTimeout;
//		this.urlCharset = urlCharset;
//		this.responseContentCharset = responseContentCharset;
//	}
//
//	/**
//	 * HTTP POST 방식 Connection을 통해 HTTP response content를 String형으로 반환한다.
//	 * 
//	 * @param requestUrl 요청 URL
//	 * @param requestParamMap 요청 파라미터
//	 * @return Content of HTTP response
//	 * @throws Exception
//	 */
//	public String httpPostConnectionAndGetContent(String requestUrl, Map<String, String> requestParamMap) throws HttpException {
//		String result = null;
//		HttpClient httpClient = new DefaultHttpClient();
//
//		try {
//			setTimeoutOfConnectionAndSocket(httpClient);
//			result = httpConnectionAndGetContent(httpClient, requestUrl, requestParamMap);
//		} catch (Exception e) {
//			throw new HttpException("Http connection fail.", e);
//		} finally {
//			closeHttpConnection(httpClient);
//		}
//
//		return result;
//
//	}
//
//	/**
//	 * HTTP response에서 content를 추출한다.
//	 * 
//	 * @param response HTTP response
//	 * @return
//	 * @throws UnsupportedEncodingException
//	 * @throws IOException
//	 */
//	/**
//	 * @param response HTTP response
//	 * @return content of HTTP response
//	 * @throws IOException
//	 */
//	private String extractContent(HttpResponse response) throws IOException {
//		String result = null;
//		BufferedReader reader = null;
//
//		try {
//			reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), responseContentCharset));
//			result = convertToString(reader);
//		} catch (IOException e) {
//			throw new IOException("It's fail that extract content from http response.");
//		} finally {
//			closeReader(reader);
//		}
//
//		return result;
//	}
//
//	/**
//	 * HTTP POST 방식 Connection을 통해 HTTP response content를 가져온다.
//	 * 
//	 * @param httpClient HttpClient
//	 * @param requestUrl 요청 URL 
//	 * @param requestParamMap 요청 파라미터
//	 * @return Content of HTTP response
//	 * @throws IOException
//	 * @throws ClientProtocolException
//	 * @throws UnsupportedEncodingException
//	 * @throws HttpException
//	 */
//	private String httpConnectionAndGetContent(HttpClient httpClient, String requestUrl, Map<String, String> requestParamMap) throws IOException, ClientProtocolException, UnsupportedEncodingException, HttpException {
//		HttpResponse response = httpClient.execute(makeHttpRequestBase(requestUrl, requestParamMap));
//		if (response == null) {
//			throw new HttpException("Http Response is null.");
//		}
//		String result = extractContent(response);
//
//		return result;
//	}
//
//	/**
//	 * HTTP connection을 닫는다.
//	 *  
//	 * @param httpClient HttpClient
//	 */
//	private void closeHttpConnection(HttpClient httpClient) {
//		try {
//			httpClient.getConnectionManager().shutdown();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//
//	/**
//	 * BufferedReader 를 close 한다.
//	 * 
//	 * @param reader BufferedReader
//	 */
//	private void closeReader(BufferedReader reader) {
//		try {
//			if (reader != null) {
//				reader.close();
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
//
//	/**
//	 * BufferedReader의 내용을 String 반환한다. 
//	 * 
//	 * @param reader BufferedReader
//	 * @return BufferedReader의 내용
//	 * @throws IOException
//	 */
//	private String convertToString(BufferedReader reader) throws IOException {
//		StringBuilder result = new StringBuilder();
//		String readLine = "";
//		while ((readLine = reader.readLine()) != null) {
//			result.append(HtmlUtils.htmlUnescape(readLine));
//		}
//
//		return result.toString();
//	}
//
//	/**
//	 * HttpClient에서 Http connection에 사용할 HttpRequestBase를 생성한다.
//	 * 
//	 * @param requestUrl 요청 URL
//	 * @param requestParamMap 요청 파라미터
//	 * @return HttpRequestBase
//	 * @throws UnsupportedEncodingException
//	 */
//	private HttpRequestBase makeHttpRequestBase(String requestUrl, Map<String, String> requestParamMap) throws UnsupportedEncodingException {
//		HttpRequestBase httpRequestBase = new HttpPost(requestUrl);
//		HttpPost post = (HttpPost)httpRequestBase;
//		post.setEntity(makeEncodedEntity(requestParamMap));
//		return httpRequestBase;
//	}
//
//	/**
//	 * 요청 파라미터를 HttpClient에서 사용할 UrlEncodedFormEntity 형태로 변환한다.
//	 * 
//	 * @param requestParamMap 요청 파라미터
//	 * @return UrlEncodedFormEntity
//	 * @throws UnsupportedEncodingException
//	 */
//	private UrlEncodedFormEntity makeEncodedEntity(Map<String, String> requestParamMap) throws UnsupportedEncodingException {
//		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//		Iterator<String> keys = requestParamMap.keySet().iterator();
//		String key = "";
//		while (keys.hasNext()) {
//			key = (String)keys.next();
//			nameValuePairs.add(new BasicNameValuePair(key, requestParamMap.get(key)));
//		}
//
//		return new UrlEncodedFormEntity(nameValuePairs, urlCharset);
//	}
//
//	/**
//	 * Http connection의 connection & socket 시간을 설정한다.
//	 * 
//	 * @param httpClient HttpClient
//	 * @param connectionTimeout connection timeout
//	 * @param socketTimeout socket timeout
//	 * @return HttpClient
//	 */
//	private void setTimeoutOfConnectionAndSocket(HttpClient httpClient) {
//		HttpParams params = httpClient.getParams();
//		HttpConnectionParams.setConnectionTimeout(params, httpConnectionTimeout);
//		HttpConnectionParams.setSoTimeout(params, httpSocketTimeout);
//	}
//}
